<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/10
 * Time: 19:18
 */
namespace IDCU;
use Think\Model as BaseModel;

class Model extends BaseModel
{

    public function getParam($req){
        $dbFields = $this->getDbFields();
        foreach($dbFields as $v){
            if(isset($req[$v]) && $req[$v]!==''){
                $param[$v] = $req[$v];
            }
        }
        return $param;
    }

    public function countDsub($dsubModelName,$where){
        $count = M($dsubModelName)->where($where)->count();
        return $count;
    }

    public function hasDsub($dsubModelName,$where){
        $count = $this->countDsub($dsubModelName,$where);
        return $count ? 1 : 0;
    }

    public function getDsup($dsupModelName,$id){
        $self = $this->find($id);
        $dsup = M($dsupModelName)->find($self[strtolower($dsupModelName).'_id']);
        return $dsup;
    }

    public function getDsupStat($dsupModelName,$id){
        $dsup = $this->getDsup($dsupModelName,$id);
        return $dsup['status'];
    }

    /**
     * 自动表单验证
     * @access protected
     * @param array $data 创建数据
     * @param string $type 创建类型
     * @return boolean
     */
    protected function autoValidation($data,$type) {
        if(!empty($this->options['validate'])) {
            $_validate   =   $this->options['validate'];
            unset($this->options['validate']);
        }elseif(!empty($this->_validate)){
            $_validate   =   $this->_validate;
        }
        // 属性验证
        if(isset($_validate)) { // 如果设置了数据自动验证则进行数据验证
            if($this->patchValidate) { // 重置验证错误信息
                $this->error = array();
            }
            foreach($_validate as $key=>$val) {
                // 验证因子定义格式
                // array(field,rule,message,condition,type,when,params)
                // 判断是否需要执行验证
                if(is_string($val[0]) && strpos($val[0],',')){
                    $val[0] = explode(',',$val[0]);
                }
                if(empty($val[5]) || ( $val[5]== self::MODEL_BOTH && $type < 3 ) || $val[5]== $type ) {
                    if(0==strpos($val[2],'{%') && strpos($val[2],'}'))
                        // 支持提示信息的多语言 使用 {%语言定义} 方式
                        $val[2]  =  L(substr($val[2],2,-1));
                    $val[3]  =  isset($val[3])?$val[3]:self::EXISTS_VALIDATE;
                    $val[4]  =  isset($val[4])?$val[4]:'regex';
                    // 判断验证条件
                    switch($val[3]) {
                        case self::MUST_VALIDATE:   // 必须验证 不管表单是否有设置该字段
                            if(false === $this->_validationField($data,$val))
                                return false;
                            break;
                        case self::VALUE_VALIDATE:    // 值不为空的时候才验证
                            if(is_array($val[0])){
                                $i = 0;
                                foreach($val[0] as $k=>$v){
                                    if('' != trim($data[$v])){
                                        $i++;
                                    }
                                }
                                if($i == count($val[0])){
                                    if(false === $this->_validationField($data,$val))
                                        return false;
                                }
                            }else{
                                if('' != trim($data[$val[0]]))
                                    if(false === $this->_validationField($data,$val))
                                        return false;
                            }
                            break;
                        default:    // 默认表单存在该字段就验证
                            if(is_array($val[0])){
                                $i = 0;
                                foreach($val[0] as $k=>$v){
                                    if(isset($data[$v])){
                                        $i++;
                                    }
                                }
                                if($i == count($val[0])){
                                    if(false === $this->_validationField($data,$val))
                                        return false;
                                }
                            }else{
                                if(isset($data[$val[0]]))
                                    if(false === $this->_validationField($data,$val))
                                        return false;
                            }
                    }
                }
            }
            // 批量验证的时候最后返回错误
            if(!empty($this->error)) return false;
        }
        return true;
    }
}